import { sessionStore } from 'store/dist/store.legacy'
const state = {
  list: {},
  indexJinde: {},
  indexMall: {},
  indexBook: {},
  indexVideo: {}
}

const mutations = {
  setThirdData (state, param) {
    sessionStore.set(`third_party_${param.str}_${param.limit}`, param.data)
    state[param.str] = param.data
  },
  setIndexJindehelp (state, param) {
    sessionStore.set('third_party_jindehelp', param.data)
    state.indexJindehelp = param.data
  },
  setIndexMall (state, param) {
    sessionStore.set('third_party_mall', param.data)
    state.indexMall = param.data
  },
  setIndexBook (state, param) {
    sessionStore.set('third_party_book', param.data)
    state.indexBook = param.data
  },
  setIndexVideo (state, param) {
    sessionStore.set('third_party_video', param.data)
    state.indexVideo = param.data
  }
}

const actions = {
  setThirdData ({ state, commit }, param) {
    let str = param.str
    if (!str) {
      return
    }
    let url
    switch (str) {
      case 'Jinde':
        url = process.env.VUE_APP_thirdPartyJindehelp_url
        break;
      case 'Jindehelp':
        url = process.env.VUE_APP_thirdPartyJinde_url
        break;
      case 'Mall':
        url = process.env.VUE_APP_thirdPartyMall_url
        break;
      case 'Book':
        url = url = process.env.VUE_APP_thirdPartyBook_url
        break;
      case 'Video':
        url = process.env.VUE_APP_thirdPartyVideo_url
    }
    let list = state[str]
    param.limit = param.limit ? param.limit : 4
    if (Object.keys(list).length == 0) {
      list = sessionStore.get(`third_party_${str}_${param.limit}`) || {}
    }

    if (Object.keys(list).length == 0) {
      window.axios.post(url, param).then(ret => {
        commit('setThirdData', { data: ret.data, str: str, limit: param.limit })
      })
    } else {
      commit('setIndexJindehelp', { data: list, str: str, limit: param.limit })
    }
  },
  setIndexJinde ({ state, commit }, param) {
    let list = state.indexJinde
    if (Object.keys(list).length == 0) {
      list = sessionStore.get('third_party_jinde') || {}
    }

    if (Object.keys(list).length == 0) {
      let url = process.env.VUE_APP_thirdPartyJinde_url
      window.axios.post(url).then(ret => {
        commit('setIndexJinde', { data: ret.data })
      })
    } else {
      commit('setIndexJinde', { data: list })
    }
  },
  setIndexMall ({ state, commit }, param) {
    let list = state.indexMall
    if (Object.keys(list).length == 0) {
      list = sessionStore.get('third_party_mall') || {}
    }
    if (Object.keys(list).length == 0) {
      let url = process.env.VUE_APP_thirdPartyMall_url
      window.axios.post(url, { limit: 6 }).then(ret => {
        commit('setIndexMall', { data: ret.data })
      })
    } else {
      commit('setIndexMall', { data: list })
    }
  },
  setIndexBook ({ state, commit }, param) {
    let list = state.indexBook
    if (Object.keys(list).length == 0) {
      list = sessionStore.get('third_party_book') || {}
    }
    if (Object.keys(list).length == 0) {
      let url = process.env.VUE_APP_thirdPartyMall_url
      window.axios.post(url, { limit: 6, cat_id: 10 }).then(ret => {
        commit('setIndexBook', { data: ret.data })
      })
    } else {
      commit('setIndexBook', { data: list })
    }
  },
  setIndexVideo ({ state, commit }, param) {
    let list = state.indexVideo
    if (Object.keys(list).length == 0) {
      list = sessionStore.get('third_party_video') || {}
    }
    if (Object.keys(list).length == 0) {
      let url = process.env.VUE_APP_thirdPartyVideo_url
      window.axios.post(url, { limit: 6 }).then(ret => {
        commit('setIndexVideo', { data: ret.data })
      })
    } else {
      commit('setIndexVideo', { data: list })
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}